package com.css.apps.base.common.tree.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AbstractAction;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;


@SuppressWarnings("serial")
public class DirOrgUserAndPostJson extends AbstractAction {
	// 机构Id
	private String name;
	private boolean showPost = true;
	private boolean showActor = true;

	protected String go() {
		JSONArray user_post_array = new JSONArray();
		if (StringHelper.isNotEmpty(name)) {
			if (showPost) {
				List<Map<String, String>> post = getPostsList(name);
				// Post , type ='GROUP'
				addPost(post, user_post_array);
			}
			if (showActor || !showPost) {
				List<Map<String, String>> user = getUsersList(name);
				// Person,type = 'ACTOR'
				addPerson(user, user_post_array);
			}
		} else {
			if (showPost) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, "请输入岗位名!");
				return ERROR;
			} else if (showActor) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, "请输入姓名!");
				return ERROR;
			}
		}
		result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, "success!", user_post_array);
		return SUCCESS;
	}

	private void addPost(List<Map<String, String>> posts, JSONArray arr) throws JSONException {
		JSONObject one;
		List<Object[]> relateIdAndOrgIdList = OrgQueryProvider.checkRelate("2");
		String relateId = null;
		String relateOrgId = null;
		if (relateIdAndOrgIdList != null) {
			for (Object[] Ids : relateIdAndOrgIdList) {
				relateId += (String) Ids[0] + ",";
				relateOrgId += (String) Ids[1] + ",";
			}
		}
		for (Map<String, String> post : posts) {
			one = new JSONObject();
			one.put("id", post.get("id"));
			one.put("name", post.get("name") + "[岗]/" + post.get("orgName"));
			one.put("org", post.get("orgId"));
			// one.put("typeid",
			// WorkFlowConstant.GROUP+"/"+orgId.trim()+"/"+post.getPostId()+"/"+WorkFlowConstant.EMPTY_PERFORMER_PART+"/"+post.getName());
			one.put("typeid", "GROUP/" + post.get("orgId") + "/" + post.get("id") + "/_");
			// one.put("relateFlag",
			// OrgProvider.checkRelate(post.get("id"),"2"));
			if (relateId != null && relateOrgId != null && relateId.indexOf(post.get("id")) != -1
					&& relateOrgId.indexOf(post.get("orgId")) != -1) {
				one.put("relateFlag", true);
			} else {
				one.put("relateFlag", false);
			}
			arr.add(one);
		}
	}

	private void addPerson(List<Map<String, String>> users, JSONArray arr) throws JSONException {
		JSONObject one;
		List<Object[]> relateIdAndOrgIdList = OrgQueryProvider.checkRelate("1");
		String relateId = null;
		String relateOrgId = null;
		if (relateIdAndOrgIdList != null) {
			for (Object[] Ids : relateIdAndOrgIdList) {
				relateId += (String) Ids[0] + ",";
				relateOrgId += (String) Ids[1] + ",";
			}
		}
		for (Map<String, String> user : users) {
			one = new JSONObject();
			one.put("id", user.get("id"));
			one.put("name", user.get("name") + "[人]/" + user.get("orgName"));
			one.put("org", user.get("orgId"));
			one.put("typeid", "ACTOR/" + user.get("orgId") + "/" + "_/" + user.get("id"));
			// one.put("relateFlag",
			// OrgProvider.checkRelate(user.get("id"),"1"));
			if (relateId != null && relateOrgId != null && relateId.indexOf(user.get("id")) != -1
					&& relateOrgId.indexOf(user.get("orgId")) != -1) {
				one.put("relateFlag", true);
			} else {
				one.put("relateFlag", false);
			}
			arr.add(one);
		}
	}

	@SuppressWarnings("unchecked")
	public List<String> getUsersByName(String userName) {
		QueryCache qc = new QueryCache(" select uuid from SUser where realName like :realName ")
				.setParameter("realName", "%" + userName + "%");
		return qc.list();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> getPostOrgByPostName(String postName) {
		QueryCache qc = new QueryCache(
				" select p.uuid,op.orgId from S_Post p left join S_Org_Post op on p.uuid=op.postId where p.name like :postName ",
				true).setParameter("postName", "%" + postName + "%");
		return qc.list();
	}

	public List<Map<String, String>> getUsersList(String key) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		// users
		List<String> users = getUsersByName(key);
		if (users == null)
			return result;
		for (String user : users) {
			SUser item = QueryCache.get(SUser.class, user);
			if (item != null) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", item.getUuid());
				map.put("name", item.getRealName());
				map.put("orgId", item.getOrgId());
				// orgname
				map.put("orgName", item.getOrgName());
				result.add(map);
			}
		}
		return result;
	}

	public List<Map<String, String>> getPostsList(String key) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		// postorgs
		List<Object[]> postorgs = getPostOrgByPostName(key);
		if (postorgs == null)
			return result;
		for (Object[] postorg : postorgs) {
			SPost postItem = QueryCache.get(SPost.class, (String) postorg[0]);
			SOrg orgItem = QueryCache.get(SOrg.class, (String) postorg[1]);
			if (postItem != null) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", postItem.getUuid());
				map.put("name", postItem.getName());
				if (orgItem != null) {
					map.put("orgId", orgItem.getOrgId());
					// orgname
					map.put("orgName", orgItem.getOrgName());
					result.add(map);
				}
			}
		}
		return result;
	}

	public boolean isShowPost() {
		return showPost;
	}

	public void setShowPost(boolean showPost) {
		this.showPost = showPost;
	}

	public boolean isShowActor() {
		return showActor;
	}

	public void setShowActor(boolean showActor) {
		this.showActor = showActor;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}